<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Channel extends CI_Controller {
	public $client = '';
	public $arrData = NULL;
	public function __construct() {
		parent::__construct();
		$this->output->set_header("Content-type: text/html; charset=utf-8");
		$this->load->model('Comm');
		$this->client = 'web/';
		$arrUserLogin = json_decode($this->Comm->getUserCookie(), true);
		$this->arrData['user_login'] = $arrUserLogin;
		$this->arrData['user_info'] = $this->Comm->getUserInfo($arrUserLogin['uid']);
		$this->arrData['channel'] = $this->Comm->getChannel();
		$channel = isset($this->uri->rsegments[3]) ? $this->uri->rsegments[3] : NULL;
		$this->arrData['selfchannel'] = $this->Comm->getChannel($this->arrData['channel'], 0, $channel);
		$this->arrData['siteinfo'] = $this->Comm->getTableCache('siteinfo', array('sid'=>1), true);
	}
	
	public function index() {
		$this->arrData['channel_depth'] = 1;
		if(isset($this->arrData['selfchannel']['parent'])) {
			if($this->arrData['selfchannel']['parent'] == '0' && $this->arrData['selfchannel']['children'] != '') {
				$this->channel();
			} else {
				$this->readingList();
			}
		} else {
			$this->showChannel();
		}
	}
	
	public function channel() {
		$this->arrData['marketing'] = $this->Comm->getTableCache('marketing', NULL, false, array('mid', 'ad'.$this->arrData['selfchannel']['cid']));
		$this->arrData['recommend'] = $this->Comm->getTableCache('marketing', NULL, false, array('mid', 'recommend'.$this->arrData['selfchannel']['cid']));
		$this->arrData['channel']   = $this->Comm->getChannel($this->arrData['channel'], $this->arrData['selfchannel']['cid']);
	    $this->arrData['__META']    = $this->Comm->getMeta($this->arrData['selfchannel']['title'].'频道页');
		$this->load->view($this->client . 'channel', $this->arrData);
	}
	
	public function search() {
		$this->readingList();
	}
	
	public function readingList() {		
		$this->arrData['arrParam'] = $this->getParam($this->uri->rsegments[4]);
		$pn = $this->arrData['arrParam']['pn'];
		$pn = empty($pn) ? 1 : $pn;
		$guide = $this->arrData['arrParam']['guide'];
		$numpage = empty($guide) ? 3 : 3;
				
		//$arrExtend['orderBy'] = 'cid asc, father_id asc, orders asc, aid asc';
		//$attribute = $this->Comm->getTableCache('attribute', NULL, false, NULL, $arrExtend);
		$arrAttribute = $this->Comm->attribute($this->arrData['selfchannel']['cid'], true);
		$attribute = $arrAttribute['attribute'];
		$this->arrData['property'] = NULL;
		
		$this->arrData['arrAttr'] = $this->arrData['arrFilter'] = $filterId = NULL;
		if(!empty($attribute)) {
			foreach($attribute as $k =>$v) {
				if($v['is_filter'] == '1' && ($v['cid'] == '0' || $v['cid'] == $this->arrData['selfchannel']['cid'])) {
					$v['filterId'] = $k;
					$this->arrData['arrAttr'][$v['aid']] = $v;
					$filterId .= $v['aid'] . ',';
				}
			}
			$filterId = trim($filterId, ',');
			if(!empty($filterId)) {
				$arrFilter = $this->Comm->getList('reading_filter','aid, values', "aid in($filterId) and cid = " . $this->arrData['selfchannel']['cid']);
				if(!empty($arrFilter)) {
					$arrAttrValue = explode('_', $this->arrData['arrParam']['attr']);
					for($i = 0; $i < 8; $i++) {if(!isset($arrAttrValue[$i])){$arrAttrValue[$i] = '';}}
					$num = count($arrFilter);
					$k = 0;
					for($i = 0; $i < $num; $i++) {
						//url list-pn-guide-attr,attr,attr,(8个)-order1,order2,order3.html
						$arrSetFilter = $arrAttrValue;
						if(urldecode($arrSetFilter[$this->arrData['arrAttr'][$arrFilter[$i]['aid']]['filterId']]) == $arrFilter[$i]['values']) {
							$arrSetFilter[$this->arrData['arrAttr'][$arrFilter[$i]['aid']]['filterId']] = '';
							$arrFilter[$i]['attr_select'] = 1;
							$unset = $i;
						} else {
							$arrSetFilter[$this->arrData['arrAttr'][$arrFilter[$i]['aid']]['filterId']] = urlencode($arrFilter[$i]['values']);
							$arrFilter[$i]['attr_select'] = 0;
							$unset = '';
						}
						$arrFilter[$i]['attr'] = implode('_', $arrSetFilter);
						$k++;
						if($k >= 50) {$k = 0; $i++;}
					}
					$this->arrData['arrFilter'] = $arrFilter;
				}
			}
			$this->arrData['property'] = $arrAttribute['property'];
		}
		$arrOrderParam = explode('_', $this->arrData['arrParam']['order']);
		for($i = 0; $i < 3; $i++) {if(!isset($arrOrderParam[$i])){$arrOrderParam[$i] = '';}}
		for($i = 0; $i < 3; $i++) {$orderBy[$i] = $arrOrderParam;}
		$arrExtend['orderBy'] = NULL;
		if($arrOrderParam[1] == 'v') {$arrExtend['orderBy'] .= 'review_times asc,';$orderBy[1][1] = '';} else {$arrExtend['orderBy'] .= 'review_times desc,';$orderBy[1][1] = 'v';}
		if($arrOrderParam[2] == 'vt') {$arrExtend['orderBy'] .= 'view_times asc,';$orderBy[2][2] = '';} else {$arrExtend['orderBy'] .= 'view_times desc,';$orderBy[2][2] = 'vt';}
		if($arrOrderParam[0] == 't') {$arrExtend['orderBy'] .= 'rid asc,';$orderBy[0][0] = '';} else {$arrExtend['orderBy'] .= 'rid desc,';$orderBy[0][0] = 't';}
		if(!empty($arrExtend['orderBy'])) $arrExtend['orderBy'] = trim($arrExtend['orderBy'],',');//print_r($arrExtend['orderBy']);
		$this->arrData['orderBy'] = $orderBy;
		
		$this->arrData['readingConut'] = $this->Comm->getCount('reading', array('is_del'=>'0'));
		$allPn = ceil($this->arrData['readingConut']/$numpage);
		$pn = $pn > $allPn ? $allPn : $pn;
		$readingList = $this->Comm->getList('reading', '*', array('is_del'=>'0'), array($pn -1, $numpage), $arrExtend);
		$num = count($readingList);
		for($i = 0; $i < $num; $i++) {
			$readingList[$i]['url'] = $this->arrData['selfchannel']['url'] . 'reading/'.$readingList[$i]['rid'].'.html';
			$readingList[$i]['html5_url'] = $this->arrData['selfchannel']['url'] . 'reader/'.$readingList[$i]['rid'].'.html';
		}
		$this->arrData['readingList'] = $readingList;
		$channelCid = empty($this->arrData['selfchannel']['children']) ? $this->arrData['selfchannel']['parent'] : $this->arrData['selfchannel']['cid'];
		$this->arrData['parallelChannel'] = $this->Comm->getChannel($this->arrData['channel'], $channelCid);
		$this->arrData['channelnav'] = $this->Comm->getChannel($this->arrData['channel'], $this->arrData['selfchannel']['parent']);
		$this->arrData['numpage'] = $numpage;
	    $this->arrData['__META'] = $this->Comm->getMeta($this->arrData['selfchannel']['title'].'频道');
		$this->arrData['allpages'] = $allPn;
		$this->arrData['pages'] = $this->Comm->getPageArray($this->arrData['allpages'], $pn);
		$this->arrData['pn'] = $pn;
		$this->arrData['guide'] = $guide;
		$this->load->view($this->client . 'list', $this->arrData);
	}
	
	public function showChannel() {
		
	}
	
	public function getParam($val) {
		//url list-pn-guide-attr,attr,attr,(8个)-order1,order2,order3.html
		$arrValue = explode('-', trim($val, '-'));
		$arrParam['pn']    = isset($arrValue[0]) && $arrValue[0] > 0 ? $arrValue[0] : NULL;
		$arrParam['guide'] = isset($arrValue[1]) ? $arrValue[1] : NULL;
		$arrParam['attr']  = isset($arrValue[2]) && !empty($arrValue[2]) ? $arrValue[2] : NULL;
		$arrParam['order'] = isset($arrValue[3]) && !empty($arrValue[3]) ? $arrValue[3] : NULL;
		return $arrParam;
	}
	
	public function setUrl($array, $key = NULL, $value = NULL) {
		if(!empty($key)) $array[$key] = $value;
		return implode('-', $array) . '.html';
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */